package com.hrm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hrm.mapper.UsersMapper;
import com.hrm.pojo.Users;
import com.hrm.pojo.DTO.UsersPageDTO;
import com.hrm.pojo.DTO.UsersUpdateDTO;
import com.hrm.service.UserService;
import com.hrm.utils.entity.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 员工登录
     *
     * @param loginname
     * @param password
     * @return
     * @throws Exception
     */
    public Users login(String loginname, String password) throws Exception {
        Users users = usersMapper.loginByName(loginname);
        if (users == null) {
            throw new Exception("用户名不存在!");
        }
        boolean flag = bCryptPasswordEncoder.matches(password, users.getPassword());
        if (!flag) {
            throw new Exception("用户名或密码错误!");
        }
        return users;
    }

    /**
     * 员工注册
     *
     * @param users
     * @return
     * @throws Exception
     */
    public Boolean register(Users users) throws Exception {
        String pwd = bCryptPasswordEncoder.encode(users.getPassword());
        System.out.println("加密后的密码:" + pwd);
        users.setPassword(pwd);
        return usersMapper.insert(users) > 0;
    }

    /**
     * 修改密码
     *
     * @param loginname
     * @param oldPwd
     * @param newPwd
     * @return
     */
    public Boolean rePwd(String loginname, String oldPwd, String newPwd) throws Exception {
        //查出当前用户的密码
        Users users = usersMapper.loginByName(loginname);
        if (oldPwd.equals(users.getPassword())) {
            throw new Exception("用户当前密码错误！");
        }
        if (newPwd == null) {
            throw new Exception("新密码不能为空");
        }
        //将加密后的新密码赋值给用户
        String password = bCryptPasswordEncoder.encode(newPwd);
        users.setPassword(password);
        return usersMapper.update(users) > 0;
    }

    /**
     * 用户列表
     *
     * @param usersPageDTO
     * @return
     */
    public PageResult<Users> page(UsersPageDTO usersPageDTO) {
        //启动分页
        Page<Users> pageUser = PageHelper.startPage(usersPageDTO.getPage(), usersPageDTO.getPageSize());
        //根据员工姓名和员工职位动态查询
        List<Users> list = usersMapper.selectByNameAndStatus(usersPageDTO.getStatus(), usersPageDTO.getLoginname());
        return new PageResult<>(pageUser.getTotal(), list);
    }

    /**
     * 删除用户
     *
     * @param id
     */
    public Boolean delete(Integer id) {
        //删除用户
        return usersMapper.deleteByPrimaryKey(id) > 0;
    }

    /**
     * 修改用户
     *
     * @param usersUpdateDTO
     * @return
     */
    public Boolean update(UsersUpdateDTO usersUpdateDTO) {
        Users users = new Users();
        BeanUtils.copyProperties(usersUpdateDTO,users);
        return usersMapper.update(users) > 0;
    }

    public Users getById(Integer id) {
        return usersMapper.getById(id);
    }
}
